class Solution
{
public:
    int maxSum(vector<int> &nums)
    {
        int n = nums.size();
        int mSum = -1;
        for (int i = 0; i < n; ++i)
        {
            for (int j = i + 1; j < n; ++j)
            {
                string s1 = to_string(nums[i]);
                string s2 = to_string(nums[j]);
                if (*max_element(s1.begin(), s1.end()) == *max_element(s2.begin(), s2.end()))
                {
                    mSum = max(mSum, nums[i] + nums[j]);
                }
            }
        }
        return mSum;
    }
};